#define _CRT_SECURE_NO_WARNINGS 1




//https://www.luogu.com.cn/problem/P1090



#include<iostream>
#include<vector>
#include<queue>

using namespace std;

typedef long long LL;

priority_queue<LL, vector<LL>, greater<LL>>heap;
int n;
int main()
{
	cin >> n;
	for (int i = 1;i <= n;++i)
	{
		int k;
		cin >> k;
		heap.push(k);
	}

	LL ret = 0;
	while (heap.size() > 1)
	{
		LL x = heap.top();heap.pop();
		LL y = heap.top();heap.pop();
		ret += x + y;
		heap.push(x + y);
	}
	cout << ret;
	return 0;
}
